package com.yenlien.traceyun.common.utils;

import java.util.Random;
import java.util.UUID;

/**
 * @author wangyang@3sqy.cn
 * @version V1.0
 * @date 2017年11月20日
 * @Description:随机数工具
 */
public class RandomUtil {

    private static char[] codeSequence = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
            '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    private static char[] num = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    public static String random(int length) {
        StringBuffer buf = new StringBuffer();
        int charLength = codeSequence.length;
        if (length >= charLength) {
            length = charLength;
        }
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            buf.append(codeSequence[random.nextInt(charLength)]);
        }
        return buf.toString();
    }

    public static String randomNum(int length) {
        StringBuffer buf = new StringBuffer();
        int charLength = num.length;
        if (length >= charLength) {
            length = charLength;
        }
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            buf.append(num[random.nextInt(charLength)]);
        }
        return buf.toString();
    }

    public static String timestampStr(int len) {
        return System.currentTimeMillis() + random(len);
    }

    /**
     * 随机长度
     */
    public static String randomStr(int len) {
        long lng = new Random().nextLong();
        if (lng < 0) {
            lng = lng * (-1);
        }
        String lngStr = "" + lng;
        if (lngStr.length() < len) {
            lngStr += random(len - lngStr.length());
        } else {
            lngStr = lngStr.substring(0, len);
        }
        return lngStr;
    }

    /**
     * uuid
     *
     * @return
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static void main(String[] args) {
//       Integer a = 0;
//       System.out.println(a.getClass().getSimpleName());
//       System.out.println(a.toString());
        StringBuilder sb = new StringBuilder("12345");
        System.out.println(System.currentTimeMillis());
    }
}
